import 'package:dio_log/overlay_draggable_button.dart';
import 'package:financing_wallet/product/product_page/view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wallet_utils/color.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wallet_utils/event_bus_utils.dart';
import 'package:wallet_utils/location/locationUtil.dart';
import 'package:wallet_utils/msg_event_utils.dart';
import 'package:wallet_utils/text_style.dart';
import 'home/home_page/view.dart';
import 'mine/mine_page/view.dart';
import 'package:get/get.dart';

class MainPage extends StatefulWidget {
  const MainPage({Key? key}) : super(key: key);

  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  List<String> titleList = [" 首页", " 产品", " 我的"];
  List<String> newTitleList = ["首页", "我的"];
  List<Image> iconList = [
    Image.asset(
      'images/ic_tab1_unchecked.png',
      width: 22.w,
      height: 22.w,
    ),
    Image.asset('images/ic_tab3_unchecked.png', width: 22.w, height: 22.w),
    Image.asset('images/ic_tab2_unchecked.png', width: 22.w, height: 22.w),
  ];
  List<Image> selectedIconList = [
    Image.asset('images/ic_tab1.png', width: 22.w, height: 22.w),
    Image.asset('images/ic_tab3.png', width: 22.w, height: 22.w),
    Image.asset('images/ic_tab2.png', width: 22.w, height: 22.w),
  ];
  List<Image> iconNewList = [
    Image.asset(
      'images/ic_tab1_unchecked.png',
      width: 22.w,
      height: 22.w,
    ),
    Image.asset('images/ic_tab2_unchecked.png', width: 22.w, height: 22.w),
  ];
  List<Image> selectedNewIconList = [
    Image.asset('images/ic_tab1.png', width: 22.w, height: 22.w),
    Image.asset('images/ic_tab2.png', width: 22.w, height: 22.w),
  ];
  int currentIndex = 0;
  List<BottomNavigationBarItem> bottomTabs = [];
  List<BottomNavigationBarItem> newBottomTabs = [];
  List<Widget> pageList = [
    HomePage(),
    ProductPage(),
    MinePage(),
  ];
  List<Widget> newPageList = [
    HomePage(),
    MinePage(),
  ];
  PageController controller = PageController();
  bool isShowProduct = false;

  @override
  void initState() {
    _init();
    EventBusUtils.getInstance().on<MsgEventBusUtils>().listen((event) {
      //是否显示产品状态
      if (event.type == MsgEventBusUtils.productStatus) {
        if (!mounted) {
          return;
        }
        setState(() {
          if (event.value == 1) {
            isShowProduct = true;
          } else if (event.value == 2) {
            isShowProduct = false;
          }
        });
      }
    });
    EventBusUtils.getInstance().on<MsgEventBusUtils>().listen((event) async {
      if (event.type == MsgEventBusUtils.upHomeStatus) {
        changeTab(0);
      }
    });
    EventBusUtils.getInstance().on<MsgEventBusUtils>().listen((event) async {
      if (event.type == MsgEventBusUtils.shopStatus) {
        if (event.value == 0) {
          changeTab(0);
        }
      }
    });
    for (var i = 0; i < titleList.length; i++) {
      bottomTabs.add(BottomNavigationBarItem(
          icon: iconList[i], activeIcon: selectedIconList[i], label: titleList[i]));
    }
    for (var i = 0; i < newTitleList.length; i++) {
      newBottomTabs.add(BottomNavigationBarItem(
          icon: iconNewList[i], activeIcon: selectedNewIconList[i], label: newTitleList[i]));
    }
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  void _init() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    String? env = await preferences.getString("ENVIRONMENT");

    if (env == "BETA" || env == "PRE") {
      //如果是bet或pre环境则显示httplog浮圈
      showDebugBtn(context, btnColor: Colors.blue);
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
        BoxConstraints(
            maxWidth: MediaQuery.of(context).size.width,
            maxHeight: MediaQuery.of(context).size.height),
        context: context,
        designSize: const Size(375, 812),
        orientation: Orientation.portrait);
    return Scaffold(
        bottomNavigationBar: BottomNavigationBar(
          unselectedFontSize: 10.sp,
          selectedFontSize: 10.sp,
          selectedItemColor: ColorManager.colorD93C3A,
          unselectedItemColor: ColorManager.color424455,
          iconSize: 22.w,
          currentIndex: currentIndex,
          type: BottomNavigationBarType.fixed,
          selectedLabelStyle:
              TextStyleUtils.styleD93C3A(fontSize: 10.w, fontWeight: FontWeight.bold),
          unselectedLabelStyle:
              TextStyleUtils.style424455(fontSize: 10.w, fontWeight: FontWeight.bold),
          backgroundColor: Colors.white,
          items: isShowProduct ? bottomTabs : newBottomTabs,
          onTap: (index) {
            changeTab(index);
          },
        ),
        body: PageView(
          physics: const NeverScrollableScrollPhysics(),
          controller: controller,
          children: isShowProduct ? pageList : newPageList,
        ));
  }

  //底部导航栏联动
  changeTab(int index) {
    setState(() {
      currentIndex = index;
      controller.jumpToPage(index);
    });
  }
}
